﻿
Imports BUS
Imports DTO
    
Public Class frmLoaiMachDieuKhien
    Private _trangThaiDangNhap As Boolean
    Public Sub TrangThai(trangthai As Boolean)
        _trangThaiDangNhap = trangthai
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If (Not txtTenLoai.Text Is Nothing) Then
            Dim dp As New LoaiMachDieuKhienBUS
            Dim tenLoai As String = txtTenLoai.Text
            Dim ret As Integer = dp.themLoaiMDK(tenLoai)
            If (ret = 1) Then
                MessageBox.Show("them thanh cong")
                dgvLoaiMDK.Rows.Clear()
                Dim ds As List(Of LoaiMachDieuKhienDTO) = dp.layDSLoaiMDK()
                For Each lmdk As LoaiMachDieuKhienDTO In ds
                    Dim row As String() = New String() {lmdk.MaLoai, lmdk.TenLoai}
                    dgvLoaiMDK.Rows.Add(row)
                Next

            End If
        End If
    End Sub

    Private Sub frmLoaiMachDieuKhien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mdiparent As Main = DirectCast(Me.MdiParent, Main)
        If mdiparent.DaDangNhap = False Then
            If MessageBox.Show("Bạn chưa nhập Mật Khẩu để thực hiện chỉnh sửa Cơ Sở Dữ Liệu. Bạn có muốn nhập Mật Khẩu không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                Dim frmMatKhau As New NhapMatKhau()
                'frmMatKhau.MdiParent = Me.MdiParent
                Dim result = frmMatKhau.ShowDialog()
                If result = Windows.Forms.DialogResult.Cancel Then
                    Me.Close()
                ElseIf result = Windows.Forms.DialogResult.OK Then

                    mdiparent.DangNhap()
                End If
            Else
                Me.Close()
            End If
        End If
        Dim dp As New LoaiMachDieuKhienBUS
        Dim ds As List(Of LoaiMachDieuKhienDTO) = dp.layDSLoaiMDK()
        For Each lmdk As LoaiMachDieuKhienDTO In ds
            Dim row As String() = New String() {lmdk.MaLoai, lmdk.TenLoai}
            dgvLoaiMDK.Rows.Add(row)
        Next

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dp As New LoaiMachDieuKhienBUS
        Dim tenLoai As String = txtTenLoai.Text
        Dim maLoai As Integer = dgvLoaiMDK.CurrentRow.Cells("MaLoai").Value.ToString()
        If (dgvLoaiMDK.SelectedRows.Count > 0 And Not txtTenLoai.Text Is Nothing) Then
            Dim ret As Integer = dp.capNhatLMDK(maLoai, tenLoai)
            MessageBox.Show("cap nhat thanh cong")
            dgvLoaiMDK.CurrentRow.Cells("tenLoai").Value = tenLoai
        End If
    End Sub

    Private Sub dgvLoaiMDK_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLoaiMDK.SelectionChanged
        txtTenLoai.Text = dgvLoaiMDK.CurrentRow.Cells("TenLoai").Value.ToString()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (dgvLoaiMDK.SelectedRows.Count > 0) Then
            Dim dp As New LoaiMachDieuKhienBUS
            Dim maLoai As Integer = Integer.Parse(dgvLoaiMDK.CurrentRow.Cells("MaLoai").Value.ToString())
            Dim ret As Integer = dp.xoaLoaiMDK(maLoai)
            If (ret = 1) Then
                MessageBox.Show("Xoa thanh cong")
                dgvLoaiMDK.Rows.Remove(dgvLoaiMDK.CurrentRow)

            End If
        End If
    End Sub
End Class